package com.fduss.artStore.dao.impl;

import com.fduss.artStore.dao.TypesStatusCodeDao;
import com.fduss.artStore.entity.TypesStatusCode;
import com.fduss.artStore.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author duocai wu
 * @Date 2017/6/24
 * @Time 21:22
 */
public class TypesStatusCodeDaoImpl implements TypesStatusCodeDao {

    private JdbcUtil util;

    public TypesStatusCodeDaoImpl() {
        this.util = new JdbcUtil();
    }

    /**
     * @param typesStatusCode
     * @return a TypesStatusCode with TypesStatusCode id
     */
    @Override
    public TypesStatusCode add(TypesStatusCode typesStatusCode) {
        if (typesStatusCode.getStatusId() != 0)
            return null;
        Connection conn = util.getConnection();
        //language=MySQL
        String sql = "INSERT INTO typesstatuscodes(Status) VALUES(?)";
        PreparedStatement pst = null;
        boolean flag = false;
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, typesStatusCode.getStatus());
            pst.executeUpdate();
            // get Id
            String idSql = "SELECT MAX(StatusID) AS StatusID FROM typesstatuscodes WHERE Status = ?";
            pst = conn.prepareStatement(idSql);
            pst.setString(1, typesStatusCode.getStatus());
            ResultSet rs = pst.executeQuery();
            if (rs.next())
                typesStatusCode.setStatusId(rs.getInt("StatusID"));
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(null, pst, conn);
        }
        return flag ? typesStatusCode : null;
    }

    @Override
    public void delete(int typesStatusCodeId) {
        Connection conn = util.getConnection();
        //language=MySQL
        String sql = "DELETE FROM typesstatuscodes WHERE StatusID = ?";
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setInt(1, typesStatusCodeId);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(null, pst, conn);
        }
    }

    @Override
    public TypesStatusCode getOne(int typesStatusCodeId) {
        Connection conn = util.getConnection();
        //language=MySQL
        String sql = "SELECT * FROM typesstatuscodes WHERE StatusID = ?";
        PreparedStatement pst = null;
        ResultSet rs = null;
        TypesStatusCode statusCode = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setInt(1, typesStatusCodeId);
            rs = pst.executeQuery();
            if (rs.next()) {
                statusCode = new TypesStatusCode(rs.getInt("StatusID"), rs.getString("Status"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(rs, pst, conn);
        }
        return statusCode;
    }

    @Override
    public List<TypesStatusCode> getAll() {
        Connection conn = util.getConnection();
        //language=MySQL
        String sql = "SELECT * FROM typesstatuscodes";
        PreparedStatement pst = null;
        ResultSet rs = null;
        List<TypesStatusCode> statusCodes = new ArrayList<>();
        try {
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            if (rs.next()) {
                statusCodes.add(new TypesStatusCode(rs.getInt("StatusID"),
                        rs.getString("Status")));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(rs, pst, conn);
        }
        return statusCodes;
    }
}
